package com.ruoyi.project.system.tongji.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.project.im.users.domain.Users;
import com.ruoyi.project.im.users.service.IUsersService;
import com.ruoyi.project.system.tongji.domain.Userexpert;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.tongji.domain.UserTongji;
import com.ruoyi.project.system.tongji.service.IUserTongjiService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 统计列表Controller
 * 
 * @author ruoyi
 * @date 2023-03-01
 */
@Controller
@RequestMapping("/system/tongji")
public class UserTongjiController extends BaseController
{
    private String prefix = "system/tongji";

    @Autowired
    private IUserTongjiService userTongjiService;

    @Autowired
    private IUsersService usersService;

    @RequiresPermissions("system:tongji:view")
    @GetMapping()
    public String tongji()
    {
        return prefix + "/tongji";
    }

    /**
     * 查询统计列表列表
     */
    @RequiresPermissions("system:tongji:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserTongji userTongji)
    {
        startPage();
        User user = getSysUser();//系统用户
        Users kuser=new Users();
        String key = user.getRoles().get(0).getRoleKey();
        if ("zikefu".equals(key)) {
            userTongji.setUpaccount(user.getLoginName());
            kuser.setCreateUser(user.getLoginName());
        }
        Date nowTime = new Date();
        List<Users> Userlist = usersService.selectUsersListByEndwithup(getSysUser().getLoginName());

        for (Users kefuuser:Userlist) {
            //未过期进行统计
                Long count=usersService.selectUsersByUserIdcount(kefuuser.getUserId());
                UserTongji userTongji1=new UserTongji();
                UserTongji userTongjitoday=userTongjiService.selectUserTongjiByIdanddate(kefuuser.getUserId());
                if(userTongjitoday==null){
                    userTongji1.setUseId(kefuuser.getUserId());
                    userTongji1.setUpaccount(kefuuser.getCreateUser());
                    userTongji1.setCounts(count);
                    userTongji1.setTongjidate(nowTime);
                    userTongji1.setUserkami(kefuuser.getKamicode());
                    userTongjiService.insertUserTongji(userTongji1);
                }else{
                    userTongji1.setCounts(count);
                    userTongjiService.updateUserTongji(userTongji1);
                }
        }
        
        List<UserTongji> list = userTongjiService.selectUserTongjiList(userTongji);
        return getDataTable(list);
    }

    /**
     * 导出统计列表列表
     */
    @RequiresPermissions("system:tongji:export")
    @Log(title = "统计列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export( Long id)
    {
        UserTongji userTongji =userTongjiService.selectUserTongjiById(id);
        //获取用户
        List<Userexpert> viuser=usersService.selectUsersListBykf(userTongji.getUseId(),userTongji.getTongjidate());
//        List<UserTongji> list = userTongjiService.selectUserTongjiList(userTongji);
        ExcelUtil<Userexpert> util = new ExcelUtil<Userexpert>(Userexpert.class);
        return util.exportExcel(viuser, "统计列表数据");
    }

    /**
     * 新增统计列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存统计列表
     */
    @RequiresPermissions("system:tongji:add")
    @Log(title = "统计列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserTongji userTongji)
    {
        return toAjax(userTongjiService.insertUserTongji(userTongji));
    }

    /**
     * 修改统计列表
     */
    @RequiresPermissions("system:tongji:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        UserTongji userTongji = userTongjiService.selectUserTongjiById(id);
        mmap.put("userTongji", userTongji);
        return prefix + "/edit";
    }

    /**
     * 修改保存统计列表
     */
    @RequiresPermissions("system:tongji:edit")
    @Log(title = "统计列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserTongji userTongji)
    {
        return toAjax(userTongjiService.updateUserTongji(userTongji));
    }

    /**
     * 删除统计列表
     */
    @RequiresPermissions("system:tongji:remove")
    @Log(title = "统计列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userTongjiService.deleteUserTongjiByIds(ids));
    }
}
